//
//  ResourceOfPriceController.m
//  lv678
//
//  Created by apple on 15/9/11.
//  Copyright (c) 2015年 lampshade. All rights reserved.
//  景点价格资源库

#import "ResourceOfPriceController.h"
#import "AFNetworking.h"
#import "ScenerySourceDataModel.h"
#import "ResourceOfPriceCell.h"
#import "SceneryOrderController.h"
#import "CommonLoading.h"

@interface ResourceOfPriceController ()
@property (nonatomic, strong) NSMutableArray *group;//接口数据可变数组
@property (nonatomic, strong) UIView *waitView;//数据加载等待视图
@end

@implementation ResourceOfPriceController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initTableView];
    [self getData];

   
}

#pragma mark -初始化视图设置
- (void)initTableView{
    UIView *waitView = [[UIView alloc]initWithFrame:CGRectZero];
    waitView = [[CommonLoading initCommonLoading]refreshLoading];
    self.waitView = waitView ;
    [self.tableView addSubview:waitView];
    self.tableView.separatorStyle = NO;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.group = [NSMutableArray array];
}

#pragma mark - 请求接口获取数据
- (void)getData{
    NSString *path = [[NSBundle mainBundle]pathForResource:@"UrlList.plist" ofType:nil];
    NSDictionary *temp = [NSDictionary dictionaryWithContentsOfFile:path];
    NSString *url = [temp valueForKey:@"viewspotapp"];
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];
    NSDictionary *data = @{@"serviceName":@"TicketResourcePriceAndInventory",@"queryParameters":
                               @{@"ResourceID":[NSNumber numberWithInt:self.resourceID],@"ProductID":[NSNumber numberWithInt:self.productID],@"DistributionChannel":@"9"}};
    [manager POST:url parameters:data success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [self.waitView removeFromSuperview];
        self.tableView.separatorStyle = YES;
        NSDictionary *temp = [[[[responseObject objectForKey:@"responseJsonObject"]objectForKey:@"resouceJsonObject"]objectForKey:@"detailJsonObject"]objectForKey:@"resourcePriceAndInventoryDailyList"];
        for (NSDictionary *dict in temp) {
            ScenerySourceDataModel *model = [ScenerySourceDataModel sourceWithDict:dict];
            [self.group addObject:model];
        }
        [self.tableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [self.waitView removeFromSuperview];
        UIView *view = [[CommonLoading initCommonLoading]noDataTips];
        [self.tableView addSubview:view];
    }];
}

#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.group.count;
}

#pragma mark - 数据源cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identity = @"reourceOfPrice";
    ScenerySourceDataModel *model = self.group[indexPath.row];
    ResourceOfPriceCell *cell = [tableView dequeueReusableCellWithIdentifier:identity];
    if (!cell) {
        cell = [[[NSBundle mainBundle]loadNibNamed:@"ResourceOfPriceCell" owner:self options:nil]lastObject];
    }
    [cell setModel:model];
    return cell;
}

#pragma mark - 点击cell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    ScenerySourceDataModel *model = self.group[indexPath.row];
    if ([self.delegate respondsToSelector:@selector(sendDate:)]) {
        [self.delegate sendDate:model];
    }
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 设置状态栏为高亮
-(UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}
@end
